package com.xl.listener;

import com.xl.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;

/**
 * @Author:XueH
 * @Description: 对socket事件监听
 * @Date:Created in 9:35 2023/11/21
 * @Modified By:
 */
@Component
@Slf4j
public class SocketEventListener {
//    @Resource
//    private ListenerService listenerService;

    /**
     * 产生连接
     *
     * @param event
     */
    @EventListener
    public void handleWebSocketConnectListener(SessionConnectedEvent event) {
        log.info("Received a new web socket connection:{}", event.toString());
//        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
//        LoginUser loginUser = (LoginUser) headerAccessor.getSessionAttributes().get(Constants.USER_INFO_KEY);
//        log.info("Received a new web socket connection ,loginUser:{}", JSON.toJSONString(loginUser));
        //listenerService.connectAction(event, null);
        System.out.println();
    }

    /***
     * 订阅消息
     * @param event
     */
    @EventListener
    public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
        String destination = (String) headerAccessor.getHeader("simpDestination");
        Object o = headerAccessor.getSessionAttributes().get(Constants.USER_INFO_KEY);
        //log.info("Socket subscribe destination:{},loginUser:{}", destination, JSON.toJSONString(loginUser));
        //listenerService.subscribeAction(event, destination, loginUser);
        System.out.println("Socket subscribe destination:{"+destination+"},loginUser:{"+o.toString()+"}");
    }

    /***
     * 取消订阅消息
     * @param event
     */
    @EventListener
    public void handleWebSocketUnSubscribeListener(SessionUnsubscribeEvent event) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
        String destination = (String) headerAccessor.getHeader("simpDestination");
        //LoginUser loginUser = (LoginUser) headerAccessor.getSessionAttributes().get(Constants.USER_INFO_KEY);
        //log.info("Socket UnSubscribe destination:{},loginUser:{}", destination, JSON.toJSONString(loginUser));
        //listenerService.unSubscribeAction(event, destination, loginUser);
        System.out.println("Socket UnSubscribe destination:{},loginUser:{}");
    }

    /**
     * 关闭连接
     *
     * @param event
     */
    @EventListener
    public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
        String destination = (String) headerAccessor.getHeader("simpDestination");
        //LoginUser loginUser = (LoginUser) headerAccessor.getSessionAttributes().get(Constants.USER_INFO_KEY);
        //log.info("Socket disconnect loginUser:{}", JSON.toJSONString(loginUser));
        System.out.println("Socket disconnect loginUser:");
        //listenerService.disconnectAction(event, destination, loginUser);
    }
}
